/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.app.command.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.itgrail.grail.cqrs.annotation.CmdHandler;
import com.itgrail.grail.cqrs.command.CommandExecutorI;
import com.itgrail.grail.dto.PageResult;
import com.itgrail.grail.dto.ResultData;
import com.itgrail.pilot.app.command.cmo.query.PageQueryAllCommentCmd;
import com.itgrail.pilot.app.command.co.AllCommentDetailCO;
import com.itgrail.pilot.app.converter.AllCommentClientConverter;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.AllCommentMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.AllCommentDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

@CmdHandler
@Slf4j
public class PageQueryAllCommentCmdExe implements CommandExecutorI<ResultData<PageResult<AllCommentDetailCO>>, PageQueryAllCommentCmd> {

    @Autowired
    private AllCommentMapper allCommentMapper;

    @Autowired
    private AllCommentClientConverter allCommentClientConverter;

    @Override
    public ResultData<PageResult<AllCommentDetailCO>> execute(PageQueryAllCommentCmd cmd) {
        ResultData<PageResult<AllCommentDetailCO>> resultData = ResultData.success(null);
        PageResult<AllCommentDetailCO> pageResult = new PageResult<>();
        if (!check(cmd)) {
            pageResult.setTotalCount(0);
            pageResult.setCurrentPage(cmd.getPageNo());
            pageResult.setList(Lists.newArrayList());
            resultData.setData(pageResult);
            return resultData;
        }
        try {
            //展示okr评论
            LambdaQueryWrapper<AllCommentDO> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(AllCommentDO::getType, 0);
            queryWrapper.eq(cmd.getPeriodId() != null, AllCommentDO::getPeriodId, cmd.getPeriodId());
            queryWrapper.eq(cmd.getOkrId() != null, AllCommentDO::getOkrId, cmd.getOkrId());
            queryWrapper.eq(AllCommentDO::getDeleted, OkrConstants.NOT_DELETED);
            queryWrapper.orderByDesc(AllCommentDO::getCreateTime);
            Long totalCount = allCommentMapper.selectCount(queryWrapper);
            totalCount = totalCount == null ? 0 : totalCount;
            if (totalCount > 0) {
                queryWrapper.last(String.format(" limit %s,%s", (cmd.getPageNo() - 1) * cmd.getPageSize(), cmd.getPageSize()));
                pageResult.setList(allCommentClientConverter.dataToClients(allCommentMapper.selectList(queryWrapper)));
            } else {
                pageResult.setList(Lists.newArrayList());
            }
            pageResult.setTotalCount(totalCount);
            pageResult.setCurrentPage(cmd.getPageNo());
            resultData.setData(pageResult);
        } catch (Exception ex) {
            log.warn(String.format("page query app failed, cmd=%s", cmd), ex);
            resultData.setCode(500);
            resultData.setMsgContent("失败");
        }
        return resultData;
    }

    protected boolean check(PageQueryAllCommentCmd cmd) {
        try {
            Preconditions.checkNotNull(cmd, "cmd不能为null");
            if (cmd.getPeriodId() == null && cmd.getOkrId() == null) {
                Preconditions.checkNotNull(null, "周期id或OKR的Id不能为空");
            }
            Preconditions.checkNotNull(cmd.getPageNo(), "pageNo不能为null");
            Preconditions.checkNotNull(cmd.getPageSize(), "pageSize不能为null");
            Preconditions.checkArgument(cmd.getPageNo() >= 1, "pageNo不能小于1");
            Preconditions.checkArgument(cmd.getPageSize() >= 1, "pageSize不能小于1");
            Preconditions.checkArgument(cmd.getPageSize() <= 500, "pageSize不能大于500");
        } catch (Exception ex) {
            return false;
        }
        return true;
    }
}
